<?php
use \think\facade\Db;

 function success($msg,$data=null){
    return json(['code'=>1,'msg'=>$msg,'data'=>$data]);
}
 function error($msg,$data=null){
    return json(['code'=>0,'msg'=>$msg,'data'=>$data]);
}

 function RandStar(){
     $str = '1234567890ABCDEFGHIJKLMNOPQISTUVWSYZ';
     $randstr = '';
     for ($i=0;$i<8;$i++){
         $randstr .=  $str[mt_rand(0,35)];
     }
        return $randstr;
 }

 function singToken($userid,$mobile){
     $data = [
         //签发人
       'iss' => request()->domain(),
         //签发时间
       'iat' => '',
         'data' => [
             'user_id' => $userid,
             'mobile' => $mobile
         ]
     ];
     $key = config('app.Jwt_key');
     $token = \Firebase\JWT\JWT::encode($data,$key,'HS256');
     return $token;
 }

function checkSmsCode($mobile, $code)
{
    $log =\think\facade\Db::name('sms_code_log')->where('mobile', $mobile)->find();
    if (!$log) {
        return false;
    }
    if ($log['code'] != $code) {
        return false;
    }
    if (time() - $log['send_time'] > 300) { // 验证码有效期5分钟
        Db::name('sms_code_log')->where('mobile', $mobile)->delete();
        return false;
    }
    Db::name('sms_code_log')->where('mobile', $mobile)->delete();
    return true;
}
